/**
 * <p>Copyright:Copyright(c) 2019</p>
 * <p>Company:Professional</p>
 * <p>Package:com.cesgroup.zw.spring.boot.echarts.model.graph</p>
 * <p>File:GraphModel.java</p>
 * <p>类更新历史信息</p>
 * @todo chanlong(陈龙) 创建于 May 28, 2019 10:16:57 AM
 */
package com.cesgroup.zw.spring.boot.echarts.model.dataset;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import com.cesgroup.zw.spring.boot.echarts.model.graph.GraphData;
import com.cesgroup.zw.spring.boot.echarts.model.graph.GraphLink;

import lombok.Data;

/**
 * <p>描述: 关系图的数据集</p>
 * <p>Company: Professional</p>
 * @author chanlong(陈龙)
 * @date May 28, 2019 10:16:57 AM
 * @version 1.0.2019
 */
@Data @SuppressWarnings("serial")
public class GraphDataset implements Serializable {
	
	public GraphDataset(GraphData data) {
		this.data = data;
		this.nodes = new ArrayList<>();
		this.links = new ArrayList<>();
	}
	
	public GraphDataset(String name, Integer category) {
		this.data = new GraphData(name, category);
		this.nodes = new ArrayList<>();
		this.links = new ArrayList<>();
	}

	/**
	 * 设置图的子节点.
	 * @param children
	 * @author chanlong(陈龙)
	 * @date May 28, 2019 12:32:30 PM
	 */
	public GraphDataset children(final Object... children) {
		String source = this.data.getName();
		Integer category = this.data.getCategory();
		for (Object child : children) {
			// 当‘child’为 String 时
			if (child instanceof String) {
				String target = (String)child;
				
				links.add(new GraphLink(source, target));
				appendNodes((String)child, category);
			// 当‘child’为 GraphData 时
			} else if (child instanceof GraphData){
				GraphData target = (GraphData)child;
				
				links.add(target.toLink(source));
				appendNodes(target.getName(), category);
			// 当‘child’为 GraphLink 时
			} else if (child instanceof GraphLink){
				GraphLink target = (GraphLink)child;
				
				links.add(target);
				appendNodes(String.valueOf(target.getTarget()), category);
			}
		}
		return this;
	}

	/**
	 * 忽略子节点<br>
	 * 创建节点关系时不同时创建节点数据.
	 * @author chanlong(陈龙)
	 * @date May 28, 2019 12:34:58 PM
	 */
	public GraphDataset ignore() {
		if (this.nodes != null) this.nodes.clear();
		return this;
	}

	// 添加节点数据
	private void appendNodes(final String name, final Integer category) {
		nodes.add(new GraphData(name, category));
	}
	
	private GraphData data;

	private List<GraphLink> links;
	
	private List<GraphData> nodes;

}
